﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 采购订单审批
    /// </summary>
    public class AppUpdateCommandHender : IRequestHandler<AppUpdateCommand, APIResult<object>>
    {

        /// <summary>
        /// 采购订单
        /// </summary>
        private readonly IBaseRepository<Purchaseorder> baseRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="baseRepository"></param>
        public AppUpdateCommandHender(IBaseRepository<Purchaseorder> baseRepository)
        {
            this.baseRepository = baseRepository;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<object>> Handle(AppUpdateCommand request, CancellationToken cancellationToken)
        {

            APIResult<object> result = new APIResult<object>();
            result.Code = APIEnum.修改成功;
            result.Msg =APIEnum.修改成功.ToString();
            var list = baseRepository.GetAll().Result.Where(X => X.PurchaseOrderId == request.PurchaseOrderId).FirstOrDefault();
            if (list.BusinessStatus == "开立")
            {
                list.BusinessStatus = "审核";
                list.AuditDate = "ZC";
            }
            else if (list.BusinessStatus == "完成")
            {
                list.BusinessStatus = "审核";
                list.AuditDate = "ZC";
            }
            else
            {
                list.BusinessStatus = "完成";
                list.AuditDate = "admin";
            }
            baseRepository.Update(list);
            return Task.FromResult(result);
        }
    }
}
